home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xsokoban / sok.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  6.2 KB  |  276 lines

  1. #include <stdio.h>
  2. #include <pwd.h>
  3. #include "sokoban.h"
  4.  
  5. extern char    *strrchr(), *getlogin(), *getpass();
  6. extern short    readscreen(), play(), outputscore(), getuserlevel(),
  7.                 makenewscore(), restoregame(), score();
  8.  
  9. short           scoring = 1;
  10. short           level, packets, savepack, moves, pushes, rows, cols;
  11. short           scorelevel, scoremoves, scorepushes;
  12. char            map[MAXROW + 1][MAXCOL + 1];
  13. POS             ppos;
  14. char           *username, *prgname;
  15.  
  16. static short    optshowscore = 0, optmakescore = 0, optrestore = 0, optlevel = 0;
  17. static short    superuser = 0;
  18.  
  19. char            bitfilepath[1024];
  20. static short    userlevel;
  21. int        optbitmap = 0, optwalls = 1;
  22.  
  23. main(argc, argv)
  24.     short           argc;
  25.     char           *argv[];
  26. {
  27.     short           ret, ret2;
  28.     struct passwd  *pwd, *getpwuid();
  29.  
  30. #ifdef VICE
  31.     Authenticate();
  32. #endif
  33.  
  34.     scorelevel = 0;
  35.     moves = pushes = packets = savepack = 0;
  36.     if ((prgname = strrchr(argv[0], '/')) == NULL)
  37.     prgname = argv[0];
  38.     else
  39.     prgname++;
  40.     if ((pwd = getpwuid(getuid())) == NULL)
  41.     ret = E_NOUSER;
  42.     else {
  43.     username = pwd->pw_name;
  44.     superuser = (strcmp(username, SUPERUSER) == 0);
  45.     if ((ret = checkcmdline(argc, argv)) == 0) {
  46.         if (optshowscore)
  47.         ret = outputscore();
  48.         else if (optmakescore) {
  49.         if (superuser) {
  50.             if ((ret = getpassword()) == 0)
  51.             ret = makenewscore();
  52.         } else
  53.             ret = E_NOSUPER;
  54.         } else if (optrestore) {
  55.         ret = restoregame();
  56.         } else if ((ret = getuserlevel(&userlevel)) == 0) {
  57.         if (optlevel > 0) {
  58.             if (superuser) {
  59.             level = optlevel;
  60.             scoring = 0;
  61.             } else if (userlevel < optlevel)
  62.             ret = E_LEVELTOOHIGH;
  63.             else
  64.             level = optlevel;
  65.         } else
  66.             level = userlevel;
  67.         }
  68.     }
  69.     }
  70.     if (ret == 0) {
  71.     ret = gameloop();
  72.     shutdown_screen();
  73.     }
  74.     errmess(ret);
  75.     if (scorelevel && scoring) {
  76.     ret2 = score();
  77.     errmess(ret2);
  78.     }
  79.     exit(ret);
  80. }
  81.  
  82. checkcmdline(argc, argv)
  83.     short           argc;
  84.     char           *argv[];
  85. {
  86.     short           ret = 0;
  87.     int             option = 0;
  88.     int             tempwalls = 0;
  89.  
  90.     for (option = 1; option < argc; option++) {
  91.     if (argv[option][0] == '-') {
  92.         switch (argv[option][1]) {
  93.         case 's':
  94.         if (optmakescore || optrestore || optlevel || optbitmap)
  95.             ret = E_USAGE;
  96.         optshowscore = 1;
  97.         break;
  98.         case 'c':
  99.         if (optshowscore || optrestore || optlevel || optbitmap)
  100.             ret = E_USAGE;
  101.         optmakescore = 1;
  102.         break;
  103.         case 'r':
  104.         if (optshowscore || optmakescore || optlevel || optbitmap)
  105.             ret = E_USAGE;
  106.         optrestore = 1;
  107.         break;
  108.         case 'b':
  109.         if (optshowscore || optmakescore || optrestore)
  110.             ret = E_USAGE;
  111.         if (argv[option][2]) {
  112.             strcpy(bitfilepath, argv[option] + 2);
  113.         } else {
  114.             option++;
  115.             strcpy(bitfilepath, argv[option]);
  116.         }
  117.         optbitmap = 1;
  118.         break;
  119.         case 'w':
  120.         if (optshowscore || optmakescore || optrestore)
  121.             ret = E_USAGE;
  122.         tempwalls = 1;
  123.         break;
  124.         default:
  125.         if (optshowscore || optmakescore || optrestore)
  126.             ret = E_USAGE;
  127.         if ((optlevel = atoi(argv[option] + 1)) == 0)
  128.             ret = E_USAGE;
  129.         break;
  130.         }
  131.     } else {
  132.         ret = E_USAGE;
  133.     }
  134.     }
  135.     if (optbitmap && !tempwalls)
  136.     optwalls = 0;
  137.     return (ret);
  138. }
  139.  
  140. gameloop()
  141. {
  142.  
  143.     short           ret = 0;
  144.  
  145.     switch (init_screen()) {
  146.     case E_NOBITMAP:
  147.     return E_NOBITMAP;
  148.     break;
  149.  
  150.     case E_NODISPLAY:
  151.     exit(1);
  152.     break;
  153.  
  154.     default:
  155.     break;
  156.     }
  157.     if (!optrestore)
  158.     ret = readscreen();
  159.     while (ret == 0) {
  160.     if ((ret = play()) == 0) {
  161.         level++;
  162.         moves = pushes = packets = savepack = 0;
  163.         ret = readscreen();
  164.     }
  165.     }
  166.     return (ret);
  167. }
  168.  
  169. getpassword()
  170. {
  171.  
  172.     return ((strcmp(getpass("Password: "), PASSWORD) == 0) ? 0 : E_ILLPASSWORD);
  173. }
  174.  
  175. char           *message[] =
  176. {
  177.     "illegal error number",
  178.     "cannot open screen file",
  179.     "more than one player position in screen file",
  180.     "illegal char in screen file",
  181.     "no player position in screenfile",
  182.     "too much rows in screen file",
  183.     "too much columns in screenfile",
  184.     "quit the game",
  185.     NULL,            /* errmessage deleted */
  186.     "cannot get your username",
  187.     "cannot open savefile",
  188.     "error writing to savefile",
  189.     "cannot stat savefile",
  190.     "error reading savefile",
  191.     "cannot restore, your savefile has been altered",
  192.     "game saved",
  193.     "too much users in score table",
  194.     "cannot open score file",
  195.     "error reading scorefile",
  196.     "error writing scorefile",
  197.     "illegal command line syntax",
  198.     "illegal password",
  199.     "level number too big in command line",
  200.     "only superuser is allowed to make a new score table",
  201.     "cannot find file to restore",
  202.     "cannot find bitmap file",
  203.     "cannot open display",
  204.     "cannot load font",
  205.     "cannot allocate string memory",
  206.     "could not load requested color"
  207. };
  208.  
  209. errmess(ret)
  210.     register short  ret;
  211. {
  212.     if (ret != E_ENDGAME) {
  213.     fprintf(stderr, "%s: ", prgname);
  214.     switch (ret) {
  215.     case E_FOPENSCREEN:
  216.     case E_PLAYPOS1:
  217.     case E_ILLCHAR:
  218.     case E_PLAYPOS2:
  219.     case E_TOMUCHROWS:
  220.     case E_TOMUCHCOLS:
  221.     case E_ENDGAME:
  222.     case E_NOUSER:
  223.     case E_FOPENSAVE:
  224.     case E_WRITESAVE:
  225.     case E_STATSAVE:
  226.     case E_READSAVE:
  227.     case E_ALTERSAVE:
  228.     case E_SAVED:
  229.     case E_TOMUCHSE:
  230.     case E_FOPENSCORE:
  231.     case E_READSCORE:
  232.     case E_WRITESCORE:
  233.     case E_USAGE:
  234.     case E_ILLPASSWORD:
  235.     case E_LEVELTOOHIGH:
  236.     case E_NOSUPER:
  237.     case E_NOSAVEFILE:
  238.     case E_NOBITMAP:
  239.     case E_NODISPLAY:
  240.     case E_NOFONT:
  241.     case E_NOMEM:
  242.     case E_NOCOLOR:
  243.         fprintf(stderr, "%s\n", message[ret]);
  244.         break;
  245.     default:
  246.         fprintf(stderr, "%s\n", message[0]);
  247.         break;
  248.     }
  249.     if (ret == E_USAGE)
  250.         usage();
  251.     }
  252. }
  253.  
  254. static char    *usagestr[] =
  255. {
  256.     "     -c             :    create new score table (superuser only)\n",
  257.     "     -r             :    restore saved game\n",
  258.     "     -s             :    show score table\n",
  259.     "     -<nn>          :    play this level (<nn> must be greater 0)\n",
  260.     "     -b <bitmap_dir>:    use alternate bitmaps. See ingame help.\n",
  261.     "                         implies !-w\n",
  262.     "     -w             :    use fancy walls (default unless you use your\n",
  263.     "                         own set of bitmaps)\n",
  264.     NULL
  265. };
  266.  
  267. usage()
  268. {
  269.  
  270.     register short  i;
  271.  
  272.     fprintf(stderr, "Usage: %s [-{s|c|r|<nn>|b <bitmap_dir>|w}]\n\n", prgname);
  273.     for (i = 0; usagestr[i] != NULL; i++)
  274.     fprintf(stderr, "%s", usagestr[i]);
  275. }
  276.